Imports DotNetNuke.Entities.Modules
Imports DotNetNuke

Imports System.Diagnostics

Namespace DGZfP.EventPublisher

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Abstract base class for reports module "sub-controls" (Visualizer
    ''' Controls, etc.)
    ''' </summary>
    ''' -----------------------------------------------------------------------------
    Public MustInherit Class PublisherModuleBase
        Inherits UserControlBase

        Private _parentModule As PortalModuleBase
        Private _localResourceFile As String


        Public Property IsControlPostback() As Boolean
            Get
                Return CBool(ViewState("ControlIsPostback"))
            End Get
            Set(ByVal value As Boolean)
                ViewState("ControlIsPostback") = value
            End Set
        End Property

        Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
            IsControlPostback = True
            MyBase.OnLoad(e)
        End Sub

        Public Property BaseModule() As PortalModuleBase
            Get
                If _parentModule Is Nothing Then
                    FindParentModule()
                End If
                Return _parentModule
            End Get
            Set(ByVal value As PortalModuleBase)
                _parentModule = value
            End Set
        End Property

        Public ReadOnly Property ModuleId() As Integer
            Get
                Debug.Assert(_parentModule IsNot Nothing, "Cannot access ModuleId if ParentModule is not set")
                Return BaseModule.ModuleId
            End Get
        End Property

        Public ReadOnly Property TabModuleId() As Integer
            Get
                Debug.Assert(_parentModule IsNot Nothing, "Cannot access ModuleId if ParentModule is not set")
                Return BaseModule.TabModuleId
            End Get
        End Property

        Public Property LocalResourceFile() As String
            Get
                If String.IsNullOrEmpty(_localResourceFile) Then
                    _localResourceFile = Me.TemplateSourceDirectory & "/" & Services.Localization.Localization.LocalResourceDirectory & "/" & ASCXFileName
                End If
                Return _localResourceFile
            End Get
            Set(ByVal value As String)
                _localResourceFile = value
            End Set
        End Property

        Public ReadOnly Property Settings() As Hashtable
            Get
                Debug.Assert(_parentModule IsNot Nothing, "Cannot access Settings if ParentModule is not set")
                Return BaseModule.Settings
            End Get
        End Property

        Protected MustOverride ReadOnly Property ASCXFileName() As String

        Private Sub FindParentModule()
            ' Iterate up the parent tree to find the parent
            Dim ctrlCurrent As Control = Me.Parent
            While ctrlCurrent IsNot Nothing AndAlso Not (TypeOf ctrlCurrent Is PortalModuleBase)
                If ctrlCurrent.Equals(ctrlCurrent.Parent) Then Return

                ctrlCurrent = ctrlCurrent.Parent
            End While
            If ctrlCurrent IsNot Nothing Then
                _parentModule = ctrlCurrent
            End If
        End Sub

    End Class

End Namespace
